package com.ziven.common.widget.title;

import android.content.Context;
import android.text.Editable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.ziven.common.widget.R;
import com.ziven.common.widget.WidgetUtils;
import com.ziven.easygo.ui.IEasyView;
import com.ziven.easygo.util.Carry;
import com.ziven.easygo.util.EasyUtils;

import java.util.Objects;

/**
 * @author zhiyuan.zhou
 */
public class SearchView extends RelativeLayout implements IEasyView {

    private EditText searchEdit;
    private TextView searchBtn;

    public SearchView(@NonNull Context context) {
        super(context);
        initLayout(context);
    }

    public SearchView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initLayout(context, attrs);
    }

    public SearchView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initLayout(context, attrs, defStyleAttr);
    }

    public SearchView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initLayout(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public void initView(@NonNull Context context, @Nullable View view) {
        final View parent = Objects.requireNonNull(view);
        searchEdit = parent.findViewById(R.id.search_view_content_edit);
        searchBtn = parent.findViewById(R.id.search_view_content_search);
    }

    @Override
    public int getChildLayoutId() {
        return R.layout.view_search_title_view;
    }

    @Override
    public ViewGroup.LayoutParams getChildLayoutParams() {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_VERTICAL);
        params.setMarginStart(WidgetUtils.dimenInt("16dp"));
        params.setMarginEnd(WidgetUtils.dimenInt("16dp"));
        return params;
    }

    public void setListener(OnClickListener l) {
        searchBtn.setOnClickListener(l);
    }

    public void setListener(Carry<String> carry) {
        searchBtn.setOnClickListener(v -> carry.carry(getText()));
    }

    @NonNull
    public String getText() {
        Editable editable = searchEdit.getText();
        String text = editable != null
                ? editable.toString()
                : null;
        return text != null ? text.trim() : EasyUtils.EMPTY;
    }

    public void setText(String text) {
        searchEdit.setText(text);
    }

    @NonNull
    public String getAndSetText(String in) {
        String out = getText();
        setText(in);
        return out;
    }
}
